home *** CD-ROM | disk | FTP | other *** search
/ Aminet 20 / Aminet 20 (1997)(GTI - Schatztruhe)[!][Aug 1997].iso / Aminet / comm / www / HTP.lha / HTP / source / textfile.c < prev    next >
C/C++ Source or Header  |  1997-06-21  |  6KB  |  282 lines

  1. /*
  2. //
  3. // textfile.c
  4. //
  5. // Text file functions
  6. //
  7. // Copyright (c) 1995-96 Jim Nelson.  Permission to distribute
  8. // granted by the author.  No warranties are made on the fitness of this
  9. // source code.
  10. // Amiga version - 1997 - Geert Bevin
  11. //
  12. */
  13.  
  14. /*
  15. // see the note in msg.c for why this is done
  16. */
  17. #define DUMB_TEXTFILE_PROTOTYPE
  18.  
  19. #include "htp.h"
  20.  
  21. BOOL OpenFile(const char *name, const char *filename, const char *openFlags,
  22.     TEXTFILE *textFile)
  23. {
  24.     assert(name != NULL);
  25.     assert(filename != NULL);
  26.     assert(openFlags != NULL);
  27.     assert(textFile != NULL);
  28.  
  29.     /* convert the filename to the native machine's format */
  30.     /* (MS-DOS to UNIX and vice-versa) */
  31.     /* this returns an allocated copy of the string, so it must be freed */
  32.     /* when the structure is destroyed */
  33.     if((textFile->filename = ConvertDirDelimiter(filename)) == NULL)
  34.     {
  35.         DEBUG_PRINT(("unable to convert dir delimiter"));
  36.         return FALSE;
  37.     }
  38.  
  39.     /* create a copy of the printed name */
  40.     if((textFile->name = DuplicateString(name)) == NULL)
  41.     {
  42.         DEBUG_PRINT(("unable to duplicate name"));
  43.         FreeMemory(textFile->filename);
  44.         textFile->filename = NULL;
  45.         return FALSE;
  46.     }
  47.  
  48.     /* initialize the rest of the structure */
  49.     textFile->lineNumber = 1;
  50.     textFile->bytesReadThisLine = 0;
  51.     textFile->bytesWrittenThisLine = 0;
  52.     textFile->flags = TEXTFILE_FLAG_NONE;
  53.  
  54.     if((textFile->file = fopen(textFile->filename, openFlags)) == NULL)
  55.     {
  56.         DEBUG_PRINT(("unable to open file %s", filename));
  57.         FreeMemory(textFile->filename);
  58.         textFile->filename = NULL;
  59.         FreeMemory(textFile->name);
  60.         textFile->name = NULL;
  61.         return FALSE;
  62.     }
  63.  
  64.     return TRUE;
  65. }   
  66.  
  67. void CreateNullFile(TEXTFILE *textFile)
  68. {
  69.     assert(textFile != NULL);
  70.  
  71.     memset(textFile, 0, sizeof(TEXTFILE));
  72.     textFile->flags = TEXTFILE_FLAG_NULL_FILE;
  73. }
  74.  
  75. void CloseFile(TEXTFILE *textFile)
  76. {
  77.     assert(textFile != NULL);
  78.  
  79.     if(textFile->flags & TEXTFILE_FLAG_NULL_FILE)
  80.     {
  81.         return;
  82.     }
  83.  
  84.     assert(textFile->filename != NULL);
  85.     assert(textFile->name != NULL);
  86.     assert(textFile->file != NULL);
  87.  
  88.     FreeMemory(textFile->filename);
  89.     FreeMemory(textFile->name);
  90.     fclose(textFile->file);
  91.  
  92.     textFile->filename = NULL;
  93.     textFile->name = NULL;
  94.     textFile->file = NULL;
  95. }   
  96.  
  97. void SuppressLinefeeds(TEXTFILE *textFile)
  98. {
  99.     textFile->flags |= TEXTFILE_FLAG_NO_CR;
  100. }
  101.  
  102. void AllowLinefeeds(TEXTFILE *textFile)
  103. {
  104.     textFile->flags &= (~TEXTFILE_FLAG_NO_CR);
  105. }
  106.  
  107. BOOL GetFileChar(TEXTFILE *textFile, char *ch)
  108. {
  109.     int getCh;
  110.  
  111.     assert(textFile != NULL);
  112.  
  113.     if(textFile->flags & TEXTFILE_FLAG_NULL_FILE)
  114.     {
  115.         return FALSE;
  116.     }
  117.  
  118.     assert(ch != NULL);
  119.     assert(textFile->file != NULL);
  120.  
  121.     do
  122.     {
  123.         if((getCh = fgetc(textFile->file)) == EOF)
  124.         {
  125.             return FALSE;
  126.         }
  127.     } while(getCh == '\r');
  128.  
  129.     if((*ch = (char) getCh) != '\n')
  130.     {
  131.         textFile->bytesReadThisLine++;
  132.     }
  133.     else
  134.     {
  135.         textFile->lineNumber++;
  136.         textFile->bytesReadThisLine = 0;
  137.         textFile->bytesWrittenThisLine = 0;
  138.     }
  139.  
  140.     return TRUE;
  141. }   
  142.  
  143. BOOL PutFileChar(TEXTFILE *textFile, char ch)
  144. {
  145.     uint bytesWrittenThisLine;
  146.  
  147.     assert(textFile != NULL);
  148.  
  149.     if(textFile->flags & TEXTFILE_FLAG_NULL_FILE)
  150.     {
  151.         return TRUE;
  152.     }
  153.  
  154.     if(ch == '\r')
  155.     {
  156.         return TRUE;
  157.     }
  158.     else if(ch == '\n')
  159.     {
  160.         bytesWrittenThisLine = textFile->bytesWrittenThisLine;
  161.  
  162.         textFile->lineNumber++;
  163.         textFile->bytesReadThisLine = 0;
  164.         textFile->bytesWrittenThisLine = 0;
  165.  
  166.         /* suppress linefeeds? */
  167.         if(textFile->flags & TEXTFILE_FLAG_NO_CR)
  168.         {
  169.             if(bytesWrittenThisLine != 0)
  170.             {
  171.                 /* emit a SPACE to avoid a run-on word from the C/R being */
  172.                 /* dropped between words */
  173.                 fputc(' ', textFile->file);
  174.             }
  175.  
  176.             return TRUE;
  177.         }
  178.     }
  179.     else
  180.     {
  181.         textFile->bytesWrittenThisLine++;
  182.     }
  183.  
  184.     assert(textFile->file != NULL);
  185.     fputc(ch, textFile->file);
  186.  
  187.     return TRUE;
  188. }   
  189.  
  190. BOOL PutFileString(va_alist) va_dcl
  191. {
  192.     va_list argptr;
  193.     char *str;
  194.     char *strptr;
  195.     TEXTFILE *textFile;
  196.     const char *format;
  197.  
  198.     /* allocate room to hold the final string ... allocating 16K to be safe, */
  199.     /* although a better method would be more deterministic */
  200.     /* (originally allocated 1K, but now allocating 16K due to JavaScript */
  201.     /* and VBScript comments ... suggested by Allan Todd) */
  202.     if((str = AllocMemory(16 * KBYTE)) == NULL)
  203.     {
  204.         return FALSE;
  205.     }
  206.  
  207.     /* convert formatted arguments into single string */
  208.     va_start(argptr);
  209.     textFile = va_arg(argptr, TEXTFILE *);
  210.     format = va_arg(argptr, const char *);
  211.     vsprintf(str, format, argptr);
  212.     va_end(argptr);
  213.     
  214.     /* check the arguments */
  215.     assert(textFile != NULL);
  216.  
  217.     if(textFile->flags & TEXTFILE_FLAG_NULL_FILE)
  218.     {
  219.         FreeMemory(str);
  220.         return TRUE;
  221.     }
  222.  
  223.     assert(format != NULL);
  224.  
  225.     strptr = str;
  226.     while(*strptr != NUL)
  227.     {
  228.         if(PutFileChar(textFile, *strptr++) == FALSE)
  229.         {
  230.             FreeMemory(str);
  231.             return FALSE;
  232.         }
  233.     }
  234.  
  235.     FreeMemory(str);
  236.  
  237.     return TRUE;
  238. }   
  239.  
  240. BOOL GetFileLine(TEXTFILE *textfile, char *buffer, uint size)
  241. {
  242.     char *ptr;
  243.  
  244.     assert(textfile != NULL);
  245.     assert(buffer != NULL);
  246.  
  247.     if(feof(textfile->file))
  248.     {
  249.         return FALSE;
  250.     }
  251.  
  252.     ptr = buffer;
  253.  
  254.     /* compare size to 1 rather than 0 to leave room for trailing NUL */
  255.     while(size-- > 1)
  256.     {
  257.         if(GetFileChar(textfile, ptr) == FALSE)
  258.         {
  259.             break;
  260.         }
  261.  
  262.         if(*ptr == '\n')
  263.         {
  264.             break;
  265.         }
  266.  
  267.         ptr++;
  268.     }
  269.  
  270.     /* watch for empty string, which should basically count as EOF */
  271.     if(buffer == ptr)
  272.     {
  273.         return FALSE;
  274.     }
  275.  
  276.     /* set the EOL to NUL */
  277.     *ptr = NUL;
  278.  
  279.     return TRUE;
  280. }
  281.  
  282.